package com.huibeili.order.service.impl;

import com.huibeili.common.utils.DateUtils;
import com.huibeili.order.domain.OmsBillingRecords;
import com.huibeili.order.mapper.OmsBillingRecordsMapper;
import com.huibeili.order.service.IOmsBillingRecordsService;
import com.huibeili.order.vo.QueryBillCriteria;
import com.huibeili.order.vo.StoreBilling;
import com.huibeili.util.PageHelper;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账单记录Service业务层处理
 *
 * @author 汇贝力商城
 * @date 2020-07-24
 */
@Service
public class OmsBillingRecordsServiceImpl implements IOmsBillingRecordsService {
    /**
     * 调试日志
     */
    private Logger logger = LoggerFactory.getLogger(OmsBillingRecordsServiceImpl.class);


    @Autowired
    private OmsBillingRecordsMapper omsBillingRecordsMapper;

    /**
     * 查询账单记录
     *
     * @param id 账单记录ID
     * @return 账单记录
     */
    @Override
    public OmsBillingRecords selectOmsBillingRecordsById(Long id) {
        return omsBillingRecordsMapper.selectOmsBillingRecordsById(id);
    }

    /**
     * 查询账单记录列表
     *
     * @param omsBillingRecords 账单记录
     * @return 账单记录
     */
    @Override
    public List<OmsBillingRecords> selectOmsBillingRecordsList(OmsBillingRecords omsBillingRecords) {
        return omsBillingRecordsMapper.selectOmsBillingRecordsList(omsBillingRecords);
    }

    /**
     * 新增账单记录
     *
     * @param omsBillingRecords 账单记录
     * @return 结果
     */
    @Override
    public int insertOmsBillingRecords(OmsBillingRecords omsBillingRecords) {
        omsBillingRecords.setCreateTime(DateUtils.getNowDate());
        return omsBillingRecordsMapper.insertOmsBillingRecords(omsBillingRecords);
    }

    /**
     * 修改账单记录
     *
     * @param omsBillingRecords 账单记录
     * @return 结果
     */
    @Override
    public int updateOmsBillingRecords(OmsBillingRecords omsBillingRecords) {
        return omsBillingRecordsMapper.updateOmsBillingRecords(omsBillingRecords);
    }

    /**
     * 批量删除账单记录
     *
     * @param ids 需要删除的账单记录ID
     * @return 结果
     */
    @Override
    public int deleteOmsBillingRecordsByIds(Long[] ids) {
        return omsBillingRecordsMapper.deleteOmsBillingRecordsByIds(ids);
    }

    /**
     * 删除账单记录信息
     *
     * @param id 账单记录ID
     * @return 结果
     */
    @Override
    public int deleteOmsBillingRecordsById(Long id) {
        return omsBillingRecordsMapper.deleteOmsBillingRecordsById(id);
    }

    @Override
    public BigDecimal queryStoreInCome(String startTime, String endTime, long storeId) {
        logger.debug("queryStoreInCome and startTime:{} \r\n endTime:{} \r\n storeId:{}", startTime, endTime, storeId);
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("storeId", storeId);

        return omsBillingRecordsMapper.queryStoreInCome(params);
    }

    @Override
    public BigDecimal queryStoreExpenses(String startTime, String endTime, long storeId) {
        logger.debug("queryStoreExpenses and startTime:{} \r\n endTime:{} \r\n storeId:{}", startTime, endTime, storeId);
        logger.debug("queryStoreInCome and startTime:{} \r\n endTime:{} \r\n storeId:{}", startTime, endTime, storeId);
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("storeId", storeId);

        return omsBillingRecordsMapper.queryStoreExpenses(params);
    }

    @Override
    public int updateStatus(Long[] ids) {
        logger.debug("updateStatus and id :{}", ids);

        if (ArrayUtils.isEmpty(ids)) {
            logger.error("updateStatus fail due to ids is empty");
            return 0;
        }

        return omsBillingRecordsMapper.updateStatus(Arrays.asList(ids));
    }

    @Override
    public PageHelper<OmsBillingRecords> queryBillingRecords(PageHelper<OmsBillingRecords> pageHelper, QueryBillCriteria queryCriteria) {
        Map<String, Object> params = queryCriteria.getQueryMap();

        return pageHelper.setListDates(omsBillingRecordsMapper.queryBillingRecords(pageHelper.getQueryParams(params, omsBillingRecordsMapper.queryBillingRecordsCount(params))));
    }

    @Override
    public PageHelper<StoreBilling> queryStoreBillings(PageHelper<StoreBilling> pageHelper, QueryBillCriteria queryCriteria) {
        logger.debug("queryStoreBillings and pageHelper:{} \r\n queryCriteria:{}", pageHelper, queryCriteria);

        Map<String, Object> params = queryCriteria.getQueryMap();

        return pageHelper.setListDates(omsBillingRecordsMapper.queryStoreBillings(pageHelper.getQueryParams(params, omsBillingRecordsMapper.queryStoreBillingsCount(params))));
    }

    @Override
    public PageHelper<OmsBillingRecords> queryStoreBillingRecords(PageHelper<OmsBillingRecords> pageHelper, QueryBillCriteria queryCriteria) {
        logger.debug("queryStoreBillingRecords and pageHelper:{} \r\n queryCriteria:{}", pageHelper, queryCriteria);


        Map<String, Object> params = queryCriteria.getQueryMap();

        return pageHelper.setListDates(omsBillingRecordsMapper.queryStoreBillingRecords(pageHelper.getQueryParams(params, omsBillingRecordsMapper.queryStoreBillingRecordsCount(params))));
    }
}
